package com.xjscrm.server.biz.impl;

import com.xjscrm.common.entity.SysTenant;
import com.xjscrm.common.enums.RedisCacheKeyEnum;
import com.xjscrm.server.mapper.SysTenantMapper;
import com.xjscrm.server.biz.SysTenantService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class SysTenantServiceImpl implements SysTenantService {
    @Autowired
    private SysTenantMapper sysTenantMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public List<SysTenant> getAll() {
        return sysTenantMapper.getAll();
    }

    @Override
    public String getTenantCodeByMerId(Long merId) {
        String key = String.format(RedisCacheKeyEnum.TENANT_CODE.getKey(), merId);
        Object tenantVal = redisTemplate.opsForValue().get(key);
        if (tenantVal==null){
            tenantVal = sysTenantMapper.getCodeByMerId(merId);
            if (tenantVal==null){
                log.info("{}:tenant is null",merId);
            }
            redisTemplate.opsForValue().set(key,tenantVal);
        }
        //log.info("{}:{}",merId,tenantVal);
        return (String) tenantVal;
    }
}
